import React from 'react';
import { Form, Input, Button, Typography, Space, message } from 'antd';
import { Link } from 'umi';
import './Register.less';

const { Title } = Typography;

interface RegisterProps {}

interface RegisterState {
  loading: boolean;
}

class Register extends React.Component<RegisterProps, RegisterState> {
  constructor(props: RegisterProps) {
    super(props);
    this.state = {
      loading: false,
    };
  }

  handleSubmit = async (values: any) => {
    this.setState({ loading: true });
    try {
      // 模拟注册请求
      if (values.password !== values.confirmPassword) {
        message.error('两次密码输入不一致');
        return;
      }

      setTimeout(() => {
        message.success('注册成功，请登录');
        this.props.history.push('/login');
      }, 1000);
    } catch (error) {
      message.error('注册失败，请稍后重试');
    } finally {
      this.setState({ loading: false });
    }
  };

  render() {
    return (
      <div className="register-container">
        <div className="register-card">
          <Title level={2} className="register-title">用户注册</Title>
          <Form
            name="registerForm"
            layout="vertical"
            onFinish={this.handleSubmit}
          >
            <Form.Item
              name="username"
              label="用户名"
              rules={[
                { required: true, message: '请输入用户名' },
                { min: 4, message: '用户名至少4个字符' },
              ]}
            >
              <Input placeholder="请输入用户名" />
            </Form.Item>

            <Form.Item
              name="password"
              label="密码"
              rules={[
                { required: true, message: '请输入密码' },
                { min: 6, message: '密码至少6个字符' },
              ]}
            >
              <Input.Password placeholder="请输入密码" />
            </Form.Item>

            <Form.Item
              name="confirmPassword"
              label="确认密码"
              dependencies={['password']}
              rules={[
                { required: true, message: '请确认密码' },
                ({ getFieldValue }) => ({
                  validator(_, value) {
                    if (!value || getFieldValue('password') === value) {
                      return Promise.resolve();
                    }
                    return Promise.reject(new Error('两次密码输入不一致'));
                  },
                }),
              ]}
            >
              <Input.Password placeholder="请确认密码" />
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                loading={this.state.loading}
                block
                size="large"
              >
                注册
              </Button>
            </Form.Item>
          </Form>

          <Space className="register-login-link">
            <span>已有账号？</span>
            <Link to="/login">前往登录</Link>
          </Space>
        </div>
      </div>
    );
  }
}

export default Register;